#include "DDArr.h"
#include "New.h"

void *access(_prop *ar,size_t x,size_t y){
	return (void *)(((char *)ar->data) + sizeof(ar->size) * (x + ar->width * y));
}

void copy_ddarr(_prop *ar1, _prop *ar2, size_t startx, size_t starty){
	if (ar1->size <= ar2->size){
		int i,j;
		void *now1;
		void *now2;
		for(i = 0 ; i < ar1->height ; i++ ){
			if (i == ar2->height){
				break;
			} else {
				for (j = 0 ; j < ar1->width ; j++){
					if (j + startx < ar2->width){
						now1 = access( ar1 , j , i );
						now2 = access( ar2 , j + startx , i + starty);
						memcpy(now2, now1, ar1->size);
					} else 
						break;
				}
			}
		}
	}
}


void reload_ddarr(_prop *ar, size_t new_height, size_t new_width){
	_prop *new_arr = new(&prop, ar->size, new_width, new_height);
	void* old_arr;
	copy_ddarr(ar,new_arr,0,0);
	old_arr = ar->data;
	ar->data = new_arr->data;
	ar->height = new_arr->height;
	ar->size = new_arr->size;
	ar->width = new_arr->width;
	new_arr->data = old_arr;
	delete(new_arr);
}